<?php

namespace App\Http\Controllers\Api\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\Admin\BaseController;
use App\Http\Requests\Folder\Store;
use App\Http\Requests\Folder\Update;
use App\Models\Folder;

class FolderController extends BaseController
{
    private $return = array(
        'code' => 1,
        'msg' => ''
    );
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,Folder $folder)
    {
        $parent = $request->input('parent',0);

        // 三、生成树形数据
        $folders = $folder->where('parent',$parent)->orderByRaw('CONVERT(name USING gbk)')->get();
        if (empty($folder)) {
            $this->return['msg'] = '暂无数据！';
            return response()->json($this->return);
        }
        $data = array();
        foreach ($folders as $folder) {
            $temp = $folder->getTree([], 'child', ['id', 'name']);
            $data[] = $temp[0];
        }
        $this->return['code'] = 0;
        $this->return['data'] = $data;
        return response()->json($this->return);
    }

    /**
     * [store description]
     * @param  Store  $request [description]
     * @param  Folder $folder  [description]
     * @return [type]          [description]
     */
    public function store(Store $request,Folder $folder){
        $parent = $request->input('parent',0);
        $data = array(
            'name' => $request->input('name')
        );
        if (!$parent) {
            // 一、创建根目录
            $folder->makeRoot();
            $res = $folder->create($data);
        }else{
            // 二、选取根目录,添加节点
            $folder = $folder->find($parent);
            if (empty($folder)) {
                $this->return['msg'] = '传参错误01！';
                return response()->json($this->return);
            }
            $res = $folder->createChild($data);
        }
        if ($res) {
            $this->return['code'] = 0;
            $this->return['msg'] = '操作成功！';
        }else{
            $this->return['msg'] = '操作失败！';
        }
        return response()->json($this->return);
    }

    /**
     * [update description]
     * @param  Update $request [description]
     * @param  Folder $folder  [description]
     * @param  [type] $id      [description]
     * @return [type]          [description]
     */
    public function update(Update $request,Folder $folder,$id){
        $data = $request->all();
        $parent = $request->input('parent',0);
        if ($parent) {
            $folder = $folder->findOrFail($parent);
            if (empty($folder)) {
                $this->return['msg'] = '传参错误01！';
                return response()->json($this->return);
            }
            $result = $folder->addChild($id);
        }else{
            $result = $folder->edit_data($id, $data);
        }
        if (!$result) {
            $this->return['msg'] = '修改失败！';
        } else {
            $this->return['code'] = 0;
            $this->return['msg'] = '修改成功！';
        }
        return response()->json($this->return);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id,Folder $folder)
    {
        $folder = $folder->find($id);
        if (!$folder) {
            $this->return['msg'] = '删除失败01！';
            return response()->json($this->return);
        }
        $folder_id_arr = $folder->getDescendantsAndSelf(['id'])->toArray();
        $collection = collect($folder_id_arr);
        $id_arr = $collection->pluck('id')->all();
        $result = $folder->delData($id_arr);
        if ($result) {
            $this->return['code'] = 0;
            $this->return['msg'] = '删除成功！';
        }else {
            $this->return['msg'] = '删除失败！';
        }
        return response()->json($this->return);
    }

}
